unit CreateIndexUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, TcpIndexUnit, ExtCtrls, ConstUnit;

type
  TfrmCreateIndex = class(TForm)
    btnAbort: TButton;
    mmoLog: TMemo;
    tmrLimit: TTimer;
    procedure btnAbortClick(Sender: TObject);
    procedure tmrLimitTimer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    FLimitRect: TRect;
    FCreateIndex: TCreateIndexFile;
    FFolderSize: Int64;
    procedure OnCreateIndex(Sender: TObject; AStatus: Widestring);

    procedure WMCreateIndex(var AMessage: TMessage); message WM_CreateIndex;
  public
    { Public declarations }
    Folder, IndexFile: string;
    function CreateFolderIndex(APath, AIndexFile: string): Int64;
    property FolderAllSize: Int64 read FFolderSize;
  end;

var
  frmCreateIndex: TfrmCreateIndex;

implementation

uses MainUnit;

{$R *.dfm}

{ TfrmCreateIndex }

function TfrmCreateIndex.CreateFolderIndex(APath, AIndexFile: string): Int64;
begin
  Caption := Format('Search%s...', [APath]);
  FCreateIndex := TCreateIndexFile.Create(APath, AIndexFile, Result);
  try
    FCreateIndex.OnCreate := OnCreateIndex;
    FCreateIndex.DoStart;
  finally
    FCreateIndex.Free;
  end;
  ModalResult := mrOk;
end;

procedure TfrmCreateIndex.OnCreateIndex(Sender: TObject; AStatus: widestring);
begin
  mmoLog.Lines.Add( AStatus );
end;

procedure TfrmCreateIndex.btnAbortClick(Sender: TObject);
begin
  if Assigned( FCreateIndex ) then
    FCreateIndex.DoAbort;
  Close;
end;

procedure TfrmCreateIndex.tmrLimitTimer(Sender: TObject);
begin
//  Windows.ClipCursor(@FLimitRect)
  tmrLimit.Enabled := False;
  FFolderSize := CreateFolderIndex(Folder, IndexFile);
end;

procedure TfrmCreateIndex.FormCreate(Sender: TObject);
begin
  frmMain.SE.AddForm(Self);
  FLimitRect.Left := (Screen.Width - Self.ClientWidth) div 2;
  FLimitRect.Top := (Screen.Height - Self.ClientHeight) div 2 + 9;
  FLimitRect.Right := FLimitRect.Left + Self.ClientWidth;
  FLimitRect.Bottom := FLimitRect.Top + Self.ClientHeight;
end;

procedure TfrmCreateIndex.FormDestroy(Sender: TObject);
begin
  Windows.ClipCursor(0);
end;

procedure TfrmCreateIndex.WMCreateIndex(var AMessage: TMessage);
var
  Folder, IndexFile: PChar;
begin
  Folder := Pointer( AMessage.WParam );
  IndexFile := Pointer( AMessage.LParam);
  FFolderSize := CreateFolderIndex(Folder, IndexFile);
end;

procedure TfrmCreateIndex.FormShow(Sender: TObject);
begin
//  SetCursorPos(Screen.Width div 2, Screen.Height div 2);
//  tmrLimitTimer(Self);
  tmrLimit.Enabled := True;
end;

end.
